﻿using System;

using BizElements.Core.ProviderComponents;

namespace BizElements.Core
{
    /// <summary>
    /// Column and expression which computes the value that will be inserted in the column.
    /// </summary>
    [Serializable]
    public sealed class InsertExpression
    {
        // As a conveniance, insert items are internaly represented as instances of PredicateItem class.
        // It is expected that all DBMSs render predicate and insert items in the same manner.

        #region CTor.

        /// <summary>
        /// Creates a new column-value pair.
        /// </summary>
        /// <param name="column">Column to which the value is to be inserted.</param>
        /// <param name="value">Value. May be <b>null</b>.</param>
        internal InsertExpression(IDbColumn column, object value)
        {
            if (value != null)
                this.ValueExpression = new PredicateItem(value, column.DbType, column.MaxLengthIfText, column.GetPrefixedAlias());
            else
                this.ValueExpression = new PredicateItem(DBNull.Value, column.DbType, column.MaxLengthIfText, column.GetPrefixedAlias());

            this.Column = column;
        }

        #endregion

        #region Properties.

        /// <summary>
        /// Gets the column to which the value is to be inserted.
        /// </summary>
        public IDbColumn Column { get; private set; }

        /// <summary>
        /// Gets the expression that computes/contains the value that is inserted in the given column.
        /// </summary>
        public IRenderSql ValueExpression { get; private set; }

        #endregion
    }	
}